package www.yingdi365.com.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpHeaders;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;
import www.yingdi365.com.R;
import www.yingdi365.com.base.ZjbBaseActivity;
import www.yingdi365.com.constant.Constant;
import www.yingdi365.com.model.AppECardSave;
import www.yingdi365.com.model.AppECardText;
import www.yingdi365.com.model.AppECardTmpl;
import www.yingdi365.com.model.EcardSave;
import www.yingdi365.com.model.OkObject;
import www.yingdi365.com.util.DpUtils;
import www.yingdi365.com.util.GsonUtils;
import www.yingdi365.com.util.LogUtil;
import www.yingdi365.com.util.ScreenUtils;

public class BianJiHKActivity extends ZjbBaseActivity implements View.OnClickListener {

    private AbsoluteLayout viewAbsolute;
    private ImageView imageBg;
    private TextView textViewRight;
    private AppECardTmpl cardBean;
    private EditText[] editTexts;
    private float baiFenBiWidth;
    private float baiFenBiHeigh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bian_ji_hk);
        init();
    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {
        Intent intent = getIntent();
        cardBean = (AppECardTmpl) intent.getSerializableExtra(Constant.INTENT_KEY.cardBean);
    }

    @Override
    protected void findID() {
        viewAbsolute = (AbsoluteLayout) findViewById(R.id.viewAbsolute);
        imageBg = (ImageView) findViewById(R.id.imageBg);
        textViewRight = (TextView) findViewById(R.id.textViewRight);
        editTexts = new EditText[cardBean.getTexts().size()];
    }

    @Override
    protected void initViews() {
        ((TextView) findViewById(R.id.textViewTitle)).setText("编辑请柬");
        textViewRight.setText("保存");
        Glide.with(BianJiHKActivity.this)
                .load(cardBean.getBgImage())
                .placeholder(R.mipmap.ic_empty)
                .into(imageBg);
        ViewGroup.LayoutParams layoutParams = viewAbsolute.getLayoutParams();
        int screenWidth = ScreenUtils.getScreenWidth(this);
        float width = screenWidth - DpUtils.convertDpToPixel(20f, this);
        float height = width * 1.4375f;//height is ready
        layoutParams.width = (int) width;
        layoutParams.height = (int) height;
        viewAbsolute.setLayoutParams(layoutParams);
        baiFenBiWidth = width / 640f;
        baiFenBiHeigh = height / 920f;
        for (int i = 0; i < editTexts.length; i++) {
            AppECardText textsBean = cardBean.getTexts().get(i);
            float vx1 = (float) (textsBean.getX()) * baiFenBiWidth;
            float vy1 = (float) (textsBean.getY()) * baiFenBiHeigh;
            float vWidth1 = (float) (textsBean.getWidth()) * baiFenBiWidth;
            float vHeight1 = (float) (textsBean.getHeight()) * baiFenBiHeigh;
            editTexts[i] = new EditText(this);
            editTexts[i].setLayoutParams(new AbsoluteLayout.LayoutParams((int) (vWidth1 * 1.2), (int) (vHeight1 * 2.2), (int) (vx1 - vWidth1 * 0.1), (int) vy1 - (int) (0.5f * vHeight1)));
            editTexts[i].setTextSize(DpUtils.convertPixelsToDp(Float.parseFloat(textsBean.getFontSize()) * baiFenBiWidth, BianJiHKActivity.this));
            editTexts[i].setText(textsBean.getContent());
            editTexts[i].setTextColor(Color.parseColor(textsBean.getColor()));
            editTexts[i].setBackgroundResource(R.drawable.shape_edittext_bianji_5dp);
            editTexts[i].setGravity(Gravity.CENTER);
            editTexts[i].setSelection(textsBean.getContent().length());
            viewAbsolute.addView(editTexts[i]);
        }
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.imageBack).setOnClickListener(this);
        textViewRight.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    private OkObject getOkObject() {
        String url = Constant.HOST + Constant.Url.AppECardService + "ecard/save";
        HttpHeaders headers = new HttpHeaders();
        headers.put(Constant.TOKEN, appLoginResult.getToken());
        HashMap<String, String> params = new HashMap<>();
        return new OkObject(headers, params, url);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageBack:
                finish();
                break;
            case R.id.textViewRight:
                showLoadingDialog();
                for (int i = 0; i < editTexts.length; i++) {
                    cardBean.getTexts().get(i).setContent(editTexts[i].getText().toString().trim());
                }
                AppECardSave appECardSave = new AppECardSave(
                        cardBean.getTexts(),
                        cardBean
                );
                OkObject okObject = getOkObject();
                OkGo.post(okObject.getUrl())//
                        .tag(this)//
                        .headers(okObject.getHeaders())
                        .upJson(GsonUtils.parseObject(appECardSave))//
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                cancelLoadingDialog();
                                LogUtil.LogShitou("BianJiHKActivity--新建贺卡", "" + s);
                                try {
                                    EcardSave ecardSave = GsonUtils.parseJSON(s, EcardSave.class);
                                    Toast.makeText(BianJiHKActivity.this, ecardSave.getMessage(), Toast.LENGTH_SHORT).show();
                                    if (ecardSave.isSuccess()) {
                                        Intent intent = new Intent();
                                        intent.setClass(BianJiHKActivity.this, WoDeHKActivity.class);
                                        startActivity(intent);
                                    }
                                } catch (Exception e) {

                                    Toast.makeText(BianJiHKActivity.this, getResources().getString(R.string.shujuchucuo), Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                super.onError(call, response, e);
                                cancelLoadingDialog();
                                if (response != null) {
                                    if (response.code() == 401) {
                                        showReLoginDialog();
                                    }
                                }
                                Toast.makeText(BianJiHKActivity.this, getResources().getString(R.string.qingqiushibai), Toast.LENGTH_SHORT).show();
                            }
                        });
                break;
        }
    }
}
